.PHONY: all force clean

ifeq (${P_BASE},)
all:
	@echo "ERROR: the variable [P_BASE] is not set"
else
include ${P_BASE}/global/utils/make/target
include ${P_BASE}/global/utils/make/local
include ${P_BASE}/global/utils/make/cmds
include ${P_BASE}/global/utils/make/escape

MOD_DIRS    = $(shell $(call C_FIND,.,-maxdepth 1 -type d) | $(call C_SED,-e "s|\./*||g"))
MOD_TARGETS = $(addprefix ${T_BASE}/mod_,${MOD_DIRS})

all: ${T_BASE}/modules
endif

${T_BASE}/mod_%: % %/Makefile force
	@$(call C_ECHO,MAKE MOD $< WITH CUSTOM MAKEFILE)
	${V}$(call C_SETENV,MOD_NAME,$<) $(call C_MAKE,$<,)
	@$(call C_ECHO,FINISH MOD $<)

${T_BASE}/mod_%: % force
	@$(call C_ECHO,MAKE MOD $< WITH MAKEFRAG.MK)
	${V}$(call C_SETENV,MOD_NAME,$<) $(call C_MAKE,$<,-f ../Makefrag.mk)
	@$(call C_ECHO,FINISH MOD $<)

${T_BASE}/mk_mod_package: mk_mod_package.c
	@$(call C_ECHO,CC $<)
	${V}$(call L_CC,$<,$@,)

${T_BASE}/modules: ${MOD_TARGETS} ${T_BASE}/mk_mod_package
	@$(call C_ECHO,MAKE $@)
	${V}$(call C_ECHO,$(MOD_TARGETS))                     | sort > ${T_BASE}/mod_files.list
	${V}$(call C_ECHO,$(addprefix __module_,${MOD_DIRS})) | sort > ${T_BASE}/mod_names.list
	${V}${T_BASE}/mk_mod_package ${T_BASE}/mod_files.list ${T_BASE}/mod_names.list $@
	${V}$(call C_RM,${T_BASE}/mod_files.list ${T_BASE}/mod_names.list)

force:
